package cn.Mmovie.servlet;

import cn.Mmovie.bean.Auditorium;
import cn.Mmovie.bean.Cinema;
import cn.Mmovie.bean.Movie;
import cn.Mmovie.bean.Schedule;
import cn.Mmovie.service.AuditoriumService;
import cn.Mmovie.service.ScheduleService;
import cn.Mmovie.service.impl.AuditoriumServiceImpl;
import cn.Mmovie.service.impl.ScheduleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Timer;

@WebServlet(name = "AdminScheduleAddServlet", value = "/admin/schedule_add")
public class AdminScheduleAddServlet extends HttpServlet {
    private ScheduleService scheduleService=new ScheduleServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Integer movieId= Integer.valueOf(request.getParameter("movie"));
        Integer auditoriumId= Integer.valueOf(request.getParameter("auditorium"));
        Time startTime= Time.valueOf(request.getParameter("actTime")+":00");
        Date startDate= Date.valueOf(request.getParameter("actDate"));
        Schedule schedule=new Schedule(new Movie(movieId),new Auditorium(auditoriumId),startTime,startDate);
        Integer res=scheduleService.addSchedule(schedule);
        if (res>0){
            request.setAttribute("msg","放映计划添加成功！");
            request.getRequestDispatcher("/admin/schedule_list").forward(request,response);
        }else if (res==-1){
            request.setAttribute("failMsg","放映计划已存在，请重试");
            request.setAttribute("schedule",schedule);
            request.getRequestDispatcher("/admin/add_schedule").forward(request,response);
        }else {
            request.setAttribute("failMsg","放映计划添加失败！");
            request.setAttribute("schedule",schedule);
            request.getRequestDispatcher("/admin/add_schedule").forward(request,response);
        }
    }
}
